package com.boc.langxiang.presenter.common;

import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.contract.common.AccountUpdatePasswordContract;
import com.boc.langxiang.entity.request.AccountUpdatePasswordRequest;
import com.boc.langxiang.entity.response.AccountUpdatePasswordContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class AccountUpdatePasswordPresenter implements AccountUpdatePasswordContract.Presenter {

    private final ApiService mService;
    private AccountUpdatePasswordContract.View mView;

    public AccountUpdatePasswordPresenter(AccountUpdatePasswordContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }

    @Override
    public void accountUpdatePassword(String oldpwd, String pwd, String pwdconf) {

        mView.showLoading();
        AccountUpdatePasswordRequest request = new AccountUpdatePasswordRequest();
        request.oldpwd = oldpwd;
        request.pwd = pwd;
        request.pwdconf = pwdconf;
        Observable<BaseResponse<AccountUpdatePasswordContent>> accountUpdatePassword = mService.accountUpdatePassword(request.params());
        accountUpdatePassword.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()).subscribe(new HandleErrorSubscriber<BaseResponse<AccountUpdatePasswordContent>>(mView) {
            @Override
            public void onSuccess(BaseResponse<AccountUpdatePasswordContent> response) {
                Toast.makeText(App.getInstance(), "密码修改成功", Toast.LENGTH_SHORT).show();
                mView.startToLoginActivity();
            }
        });
    }
}
